package com.becca.cake.service.system.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.FunctionDAO;
import com.becca.cake.model.system.FunctionDO;
import com.becca.cake.model.system.MenuTree;
import com.becca.cake.model.system.Node;
import com.becca.cake.service.system.IUserFunctionService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.PMSConstants;

/**
 * 功能管理Service实现类
 * @author hejinguo
 * @version $Id: UserFunctionServiceImpl.java, v 0.1 2013-12-3 下午01:50:11
 */
@Service
public class UserFunctionServiceImpl implements IUserFunctionService {
    @Autowired
    private FunctionDAO functionDAO;

    /**
     * 设置缓存
     */
    @Override
    @Cacheable(key = "'selectFunctionTreeMenu'+#isUnable", value = "monmondecakeCache")
    public String selectFunctionTreeMenu(String isUnable) throws Exception {
        //功能树顶级ID
        Long id = Long.valueOf(PMSConstants.FUNCTION_ROOT);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("isUnable", isUnable);//是否查询出已被禁用的功能 0：否 1：是
        //根据员工权限获取员工功能
        List<Node> functions = this.functionDAO.selectAllFunctionById(map);
        //根据ID查询当前功能
        Node node = this.functionDAO.selectFunctionById(id);
        //封装菜单树json
        MenuTree tree = new MenuTree();
        tree.getTree(functions, node, false);
        return tree.modifyStr(tree.returnStr.toString());
    }

    @Override
    public String selectFunctionDOById(String id) throws Exception {
        FunctionDO functionDO = this.functionDAO.selectFunctionDOById(id);
        return JsonUtil.writeObjectFormJSON(functionDO);
    }

    /**
     * 清除缓存
     */
    @Override
    @CacheEvict(key = "'selectFunctionTreeMenu'+#functionDO.getIsUseable()", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insetFunction(FunctionDO functionDO) throws Exception {
        this.functionDAO.insetFunction(functionDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateFunctionById(FunctionDO functionDO) throws Exception {
        this.functionDAO.updateFunctionById(functionDO);
    }

    /**
     * 设置缓存
     */
    @Override
    @Cacheable(key = "'selectOwnFunctionByRoleId'+#roleId", value = "monmondecakeCache")
    public String selectOwnFunctionByRoleId(String roleId) throws Exception {
        return this.functionDAO.selectOwnFunctionByRoleId(roleId);
    }

    @Override
    @CacheEvict(key = "'selectOwnFunctionByRoleId'+#map.get('roleId')", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateGrantRoleFunction(Map<String, Object> map) throws Exception {
        //删除角色功能信息
        this.functionDAO.deleteRoleFunctionByRoleId(map.get("roleId").toString());
        //添加角色功能信息
        this.functionDAO.insertRoleFunction(map);
    }

    @Override
    public String selectFunctionByAdminId(String adminId) throws Exception {
        //功能树顶级ID
        Long id = Long.valueOf(PMSConstants.FUNCTION_ROOT);
        //根据员工权限获取员工功能
        List<Node> functions = this.functionDAO.selectFunctionByAdminId(adminId);
        //根据ID查询当前功能
        Node node = this.functionDAO.selectFunctionById(id);
        //封装菜单树json
        MenuTree tree = new MenuTree();
        tree.getTree(functions, node, false);
        return tree.modifyStr(tree.returnStr.toString());
    }
}
